package com.sq.partner.manager.dao.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sq.partner.manager.mybatis.model.GatewayMenu;
import com.sq.partner.manager.mybatis.model.GatewayScanUnionMenu;
import org.springframework.stereotype.Repository;

import com.sq.partner.manager.dao.UnionScanManagerDao;
import com.sq.partner.manager.mybatis.model.GatewayScanUnionCode;
import com.sq.partner.manager.util.Page;
import org.springframework.util.StringUtils;

@Repository
public class UnionScanManagerDaoImpl extends BaseDaoImpl implements UnionScanManagerDao {

	/* (non-Javadoc)
	 * @see com.sq.partner.manager.dao.unionScanManagerDao#queryUnionQRCodeListByPageNumber(com.sq.partner.manager.util.Page)
	 * @sql：
	 */
	@Override
	public Page<GatewayScanUnionCode> queryUnionQRCodeListByPageNumber(Page<GatewayScanUnionCode> page) throws Exception {
		List<GatewayScanUnionCode> rsMap = this.queryList("queryUnionQRCodeListByPageNumber", page.getMap());
		page.setData(rsMap);
		return page;
	}

	@Override
	public int queryUnionCodeCountByConditions(Page<GatewayScanUnionCode> page) throws Exception {

		return (Integer)this.query("queryUnionCodeCountByConditions",page.getMap());
	}

	@Override
	public int queryUnionCodeCanBeCinfigCountByConditions(Page<Map<String, Object>> page) throws Exception {

		int count = (Integer)this.query("queryUnionCodeCanBeCinfigCountByConditions", page.getMap());
		
		return count;
	}

	@Override
	public Page<Map<String, Object>> queryUnionCodeCanBeConfigByPageNumber(Page<Map<String, Object>> page)
			throws Exception {
		List<Map<String,Object>> list = this.queryList("queryUnionCodeCanBeConfigByPageNumber", page.getMap());
		page.setData(list);
		return page;
	}

	@Override
	public int queryMenuCanBeCinfigCountByConditions(Page<Map<String, Object>> page) throws Exception {

		int count = (Integer)this.query("queryMenuCanBeCinfigCountByConditions", page.getMap());

		return count;
	}

	@Override
	public Page<Map<String, Object>> queryMenuCanBeConfigByPageNumber(Page<Map<String, Object>> page)
			throws Exception {
		List<Map<String,Object>> list = this.queryList("queryMenuCanBeConfigByPageNumber", page.getMap());
		page.setData(list);
		return page;
	}


	@Override
	public Page<Map<String, Object>> queryConfigedUnionCodeByPage(Page<Map<String, Object>> page) throws Exception {
		List<Map<String,Object>> list = this.queryList("queryConfigedUnionCodeByPage", page.getMap());
//		List<Map<String,Object>> list = this.queryList("queryConfigedMenuByPage", page.getMap());
		page.setData(list);
		return page;
	}

	@Override
	public Page<Map<String, Object>> queryConfigedMenuByPage(Page<Map<String, Object>> page) throws Exception {
//		List<Map<String,Object>> list = this.queryList("queryConfigedUnionCodeByPage", page.getMap());
		List<Map<String,Object>> list = this.queryList("queryConfigedMenuByPage", page.getMap());
		page.setData(list);
		return page;
	}

	@Override
	public int queryConfigedUnionCodeCountByPage(Page<Map<String, Object>> page)
			throws Exception {
		return (Integer)this.query("queryConfigedUnionCodeCountByPage", page.getMap());
//		return (Integer)this.query("queryConfigedMenuCountByPage", page.getMap());
	}

	@Override
	public int queryConfigedMenuCountByPage(Page<Map<String, Object>> page)
			throws Exception {
//		return (Integer)this.query("queryConfigedUnionCodeCountByPage", page.getMap());
		return (Integer)this.query("queryConfigedMenuCountByPage", page.getMap());
	}

	@Override
	public boolean deleteConfigedUnionCode(String id) throws Exception {
	
		return this.delete("deleteConfigedUnionCode", id);
	}


	@Override
	public boolean deleteMenuUnion(String id) throws Exception {

		return this.delete("deleteMenuUnion", id);
	}

	@Override
	public boolean insertUnionCodeRela(String[] id, String unionCodeId,String[] names,String menuId,String queryMenuLevel) throws Exception {

	/*	HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("menuId",menuId);
		String queryMenuLevel = (String)query("queryMenuLevel", hashMap);
		if (StringUtils.isEmpty(queryMenuLevel)){
			return false;
		}*/

		boolean rs = true ;
		if(id.length > 0){
			for(int i = 0 ; i < id.length ; i++){
				Map<String,Object> map = new HashMap<String, Object>();
				map.put("id", id[i]);
				map.put("name", names[i]);
//				map.put("unionCodeId", unionCodeId);
				map.put("menuId", menuId);
				map.put("queryMenuLevel",queryMenuLevel);
				boolean reslut = this.add("insertUnionCodeRela", map);
				if(!reslut){
					rs = false ;
				}
			}
		}
		return rs;
	}

	@Override
	public boolean insertMenuRela(String[] id, String menuId) throws Exception {
		boolean rs = true ;
		if(id.length > 0){
			for(int i = 0 ; i < id.length ; i++){
				Map<String,Object> map = new HashMap<String, Object>();
				map.put("id", id[i]);
				map.put("menuId", menuId);
				boolean reslut = this.add("insertMenuRela", map);
				if(!reslut){
					rs = false ;
				}
			}
		}
		return rs;
	}


	@Override
	public int addNewUnionCode(Map<String, String> param) throws Exception {
		//先获取id最大值+1，作为主键
		int menuId =  (Integer) this.query("selectMaxMenuId", param);
		int codeId =  (Integer) this.query("selectMaxCodeId", param);
		param.put("menuId", String.valueOf(menuId));
		param.put("codeId", String.valueOf(codeId));
		try{
			this.add("addNewSingleMenu", param);
			this.add("addNewUnionCode", param);
		}catch(SQLException e){
			e.printStackTrace();
			return 0 ;
		}
		return codeId;
	}

	@Override
	public int addNewMenu(Map<String, String> param) throws Exception {
		//先获取id最大值+1，作为主键
		int menuId =  (Integer) this.query("selectMaxMenuId", param);
		param.put("menuId", String.valueOf(menuId));
		try{
			this.add("addNewMenu", param);
		}catch(SQLException e){
			e.printStackTrace();
			return 0 ;
		}
		return menuId;
	}

	@Override
	public GatewayScanUnionCode querySingleUnionCodeById(int id) throws Exception {
		Map<String,Object> map =  (Map<String,Object>)this.query("querySingleUnionCodeById", id);
		
		GatewayScanUnionCode uc = new GatewayScanUnionCode();
		uc.setId(BigDecimal.valueOf(id));
		uc.setUnionCodeName((String) map.get("UNION_CODE_NAME"));
		uc.setUnionCodeDesc((String) map.get("UNION_CODE_DESC"));
		return uc;
	}

	@Override
	public boolean deleteUnionCode(String codeId) throws Exception {
		return this.delete("deleteUnionCode", codeId);
	}

	@Override
	public boolean deleteMenu(String menuId) throws Exception {
		return this.delete("deleteMenu", menuId);
	}

	@Override
	public Page<GatewayScanUnionMenu> queryMenuListByPageNumber(Page<GatewayScanUnionMenu> page) throws Exception {
		List<GatewayScanUnionMenu> rsMap = queryList("queryMenuListByPageNumber", page.getMap());
		page.setData(rsMap);
		return page;
	}

	@Override
	public int queryMenuListByConditions(Page<GatewayScanUnionMenu> page) throws Exception {
		return (Integer)this.query("queryMenuListByConditions",page.getMap());
	}

	@Override
	public boolean updateNewMenu(Map<String, String> param) throws Exception{
		return this.update("updateNewMenu",param);
	}

	@Override
	public GatewayMenu querySingleMenuById(int codeId) throws Exception {
		Map<String,Object> map =  (Map<String,Object>)this.query("querySingleMenuById", codeId);

		GatewayMenu menu = new GatewayMenu();
		menu.setMenuid(((BigDecimal)map.get("ID")).longValue());
		menu.setMenuName((String) map.get("MENU_NAME"));
		menu.setMenuDesc((String) map.get("MENU_DESC"));
		return menu;
	}

	@Override
	public String queryMenuType(String menuId) throws SQLException {
		return (String)query("queryMenuType",menuId);
	}

	@Override
	public void updateMenuType(String menuId, String type)throws SQLException {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("menuId",menuId);
		map.put("type",type);
		query("updateMenuType",map);
	}

	@Override
	public String queryMenuLevel(HashMap<String, Object> hashMap) throws SQLException {
		String queryMenuLevel = (String)query("queryMenuLevel", hashMap);
		return queryMenuLevel;
	}

	@Override
	public Integer selectPlanCountByMenuId(String menuId) throws SQLException {
		return (Integer)query("selectPlanCountByMenuId", menuId);
	}

	@Override
	public Integer selectPlanCountByUnionCode(String codeId) throws SQLException {
		return (Integer)query("selectPlanCountByUnionCode", codeId);
	}

	@Override
	public Integer selectMenuCountByUnionCode(String codeId) throws SQLException {
		return (Integer)query("selectMenuCountByUnionCode", codeId);
	}

	@Override
	public boolean deleteMenuByUnionCode(String codeId) throws Exception {
		return this.delete("deleteMenuByUnionCode", codeId);
	}

	@Override
	public Integer selectSubMenuCountByMenuId(String menuId) throws SQLException {
		return (Integer)query("selectSubMenuCountByMenuId", menuId);
	}

	@Override
	public boolean deleteUnionCodeByMenuId(String menuId) throws Exception {
		return delete("deleteUnionCodeByMenuId", menuId);
	}
}
